<?php

namespace app\api\controller;

use app\admin\model\user\UserMoneyLog as MoneyLogModel;
use app\common\model\Withdrawal as WithdrawalModel;
use app\common\controller\Api;
use think\Config;


/**
 * 会员提现接口
 */
class Withdrawal extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 提现列表
     */
    public function index()
    {
        //游客不允许请求此接口
        $user_info = $this->auth->getUser();
        if(empty($user_info)){
            return $this->error('Please login');
        }

        $page = $this->request->request('page');

        // 筛选条件
        $filter['user_id'] = $user_info['id'];

        $model_temp = new WithdrawalModel();
        $listdata = $model_temp->where($filter)->order('id desc')
            ->paginate(8, false, [ 'page' => $page ])
            ->each(function($item){

            });

        $pagedata = $listdata;
        $this->success('', [
            'pagedata' => $pagedata,
        ]);
    }

    /**
     * 新增提现
     */
    public function add()
    {
        $user = $this->auth->getUser();

        if(empty($user)){
            return $this->error('Please login');
        }
        $routing_number = $this->request->post('routing_number');
        $account_number = $this->request->post('account_number');
        $mobile = $this->request->post('mobile');
        $money = $this->request->post('money');
        $memo = $this->request->post('memo');

        $config = Config::get('site');
        if($user->money < $money){
            $this->error(__('Sorry, Insufficient commission'));
        }
        $service_charge =  $config['withdrawal_fee']/100 * $money;
        $payment_money = $money - $service_charge;

        $model_temp = new WithdrawalModel();
        $model_temp->user_id = $user->id;
        $model_temp->routing_number = $routing_number;
        $model_temp->account_number = $account_number;
        $model_temp->mobile = $mobile;
        $model_temp->money = $money;
        $model_temp->service_charge = $service_charge;
        $model_temp->payment_money = $payment_money;
        $model_temp->memo = $memo;
        $model_temp->save();

        //扣除金额
        $userModel = model('app\admin\model\User')->get(['id' => $user->id]);
        $before = $userModel->money;
        $userModel->money = $before - $money;
        $userModel->save();

        //增加记录
        (new MoneyLogModel())->add_money_log($user->id,-$money,$before,$userModel->money,'提现扣除');

        $this->success();
    }

    /**
     * 取消申请
     */
    public function cancel($id)
    {
        $model_temp = new WithdrawalModel();
        $listdata = $model_temp->where('id',$id)->where('state','10')->find();
        if(empty($listdata)){
            $this->error(__('Sorry, data error'));
        }
        $listdata->state = '20';
        $listdata->save();

        //返还金额
        $userModel = model('app\admin\model\User')->get(['id' => $model_temp->user_id]);
        $before = $userModel->money;
        $userModel->money = $before + $model_temp->money;
        $userModel->save();

        //增加记录
        (new MoneyLogModel())->add_money_log($model_temp->user_id,$model_temp->money,$before,$userModel->money,'提现取消，金额返还');

        $this->success('');
    }
}
